package content

import (
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"go-papers/models"
	"go-papers/tools"
	"go-papers/tools/app"
	"go-papers/tools/app/msg"
)

func GetSpecialContentList(c *gin.Context) {
	var data models.SpecialContent
	var err error
	var pageSize = 10
	var pageIndex = 1

	if size := c.Request.FormValue("pageSize"); size != "" {
		pageSize = tools.StrToInt(err, size)
	}
	if index := c.Request.FormValue("pageIndex"); index != "" {
		pageIndex = tools.StrToInt(err, index)
	}
	if index := c.Request.FormValue("specialId"); index != "" {
		data.SpecialId = tools.StrToInt(err, index)
	}
	data.SpecialStatus = c.Request.FormValue("specialStatus")
	data.SpecialCode = c.Request.FormValue("specialCode")
	data.SpecialName = c.Request.FormValue("specialName")
	data.SpecialBanner = c.Request.FormValue("specialBanner")
	data.SpecialStart = c.Request.FormValue("specialStart")
	data.SpecialEnd = c.Request.FormValue("specialEnd")
	data.CreateBy = c.Request.FormValue("createBy")
	data.UpdateBy = c.Request.FormValue("updateBy")

	data.DataScope = tools.GetUserIdStr(c)
	result, count, err := data.GetPage(pageSize, pageIndex)
	if err != nil {
		app.ApiResp(c, -1, "数据查询失败："+err.Error(), nil)
		return
	}
	app.PageOK(c, result, count, pageIndex, pageSize, "")
}

func GetSpecialContent(c *gin.Context) {
	var data models.SpecialContent
	data.SpecialId, _ = tools.StringToInt(c.Param("specialId"))
	result, err := data.Get()
	if err != nil {
		app.ApiResp(c, -1, "数据查询失败："+err.Error(), nil)
		return
	}
	app.OK(c, result, "")
}

func InsertSpecialContent(c *gin.Context) {
	var data models.SpecialContent
	err := c.ShouldBindJSON(&data)
	if err != nil {
		app.ApiResp(c, -1, "数据解析失败："+err.Error(), nil)
		return
	}

	data.CreateBy = tools.GetUserIdStr(c)
	result, err := data.Create()
	if err != nil {
		app.ApiResp(c, -1, "数据插入失败："+err.Error(), nil)
		return
	}
	app.OK(c, result, "")
}

func UpdateSpecialContent(c *gin.Context) {
	var data models.SpecialContent
	err := c.BindWith(&data, binding.JSON)
	if err != nil {
		app.ApiResp(c, -1, "数据解析失败："+err.Error(), nil)
		return
	}
	result, err := data.Update(data.SpecialId)
	if err != nil {
		app.ApiResp(c, -1, "数据更新失败："+err.Error(), data)
		return
	}
	app.OK(c, result, "")
}

func DeleteSpecialContent(c *gin.Context) {
	var data models.SpecialContent

	IDS := tools.IdsStrToIdsIntGroup("specialId", c)
	_, err := data.BatchDelete(IDS)
	if err != nil {
		app.ApiResp(c, -1, "数据删除失败："+err.Error(), nil)
		return
	}

	app.OK(c, nil, msg.DeletedSuccess)
}
